/**
 * 折扣模态
 */
;(function ($) {
	//折扣模态入口
	$.fn.discountDetail=function(options){
    	//创建对象
		var app = new discount(this, options);
		app.html(); 
	}
	
	//构造函数
    var discount = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
        	title:"",//标题
        	no:"",//no号
        	arg : "折扣",
    		returnBack:function(){},//返回回调函数
    		dataBack:function(){},//完成取消预授权回调函数
        },        
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }
    
    //属性方法
    discount.prototype = {
    	//构造html
    	html:function(){ 
    		var $t=this;
    		var $opt=$t.options;
    		
    		$t.$element.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static")
	    		html='<div class="modal-dialog remark_order coupon_search" style="width:600px;">'+
			    		'<div id="pageOverlay" class="hide"></div>'+
						'<div id="loading" class="loading hide">	'+
							'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
						'</div>'+
						'<div class="modal-content" style="width:600px;">'+     
				  			'<div class="modal-header no-padding">'+
								'<div class="table-header widget-header-large " style="text-align:center; font-size:20px">'+  
									'<span>'+$.i18n.propJsp($opt.title) +'</span>'+       
								'</div>'+
							'</div>'+
							'<div class="modal-body modal-coupon">'+  
								'<form id="discountSave">'+
									'<div class="input-group" id="coupon_type">'+
										'<span class="input-group-addon">'+
											$.i18n.propJsp("折扣类型：")+
										'</span>'+
										'<input type="text" class="form-control search-query" placeholder="" name="discode" readonly>'+
										'<span class="input-group-btn">'+
											'<button type="button" class="btn btn-primary btn-sm">'+
												'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
												$.i18n.propJsp("选择")+
											'</button>'+
										'</span>'+
									'</div>'+
									'<div class="input-group" id="coupon_rate">'+
										'<span class="input-group-addon">'+
											$.i18n.propJsp("折扣率：")+
										'</span>'+
										'<input type="text" class="form-control search-query" placeholder="" name="disrate">'+
										'<span class="input-group-btn">'+
											'<button type="button" class="btn btn-primary btn-sm">'+
												'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
												$.i18n.propJsp("选择")+
											'</button>'+
										'</span>'+
									'</div>'+
//									'<div class="input-group modal-remark" id="coupon_remark">'+
//										'<span class="input-group-addon">'+
//											$.i18n.propJsp("原因：")+
//										'</span>'+
//										'<textarea class="form-control search-query" placeholder="" name="disrate"></textarea>'+
//										'<span class="input-group-btn">'+
//											'<button type="button" class="btn btn-primary btn-sm">'+
//												'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
//												$.i18n.propJsp("选择")+
//											'</button>'+
//										'</span>'+
//									'</div>'+
								'</form>'+
							'</div>'+	
							'<div class="modal-body modal-list">'+  
								'<div class="orderTaste"></div>'+
							'</div>'+	
							'<div class="modal-footer">'+
								'<button id="modalBtnCancelDisrate" class="btn btn-info btn-lg btn-round">'+  
									'<i class="ace-icon fa fa-trash"></i>'+$.i18n.propJsp("取消折扣")+
								'</button>'+
								'<button id="btnSure" class="btn btn-info btn-lg btn-round">'+  
									'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
								'</button>'+
								'<button id="btnCancel" class="btn btn-default btn-lg btn-round">'+  
									'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+'</button>'+ 
								'</button>'+
							'</div>'+		
		  				'</div>'+
		  			'</div>'+		
				'</div>'+
			'</div>';
    		
    		$t.$element.html(html);
    		$t.$element.find(".remark_order,.modal-content").css("height","auto").css("min-height","255px");//.css("min-height","320px");
//    		$t.$element.find(".modal-list").css({"height":$t.$element.find(".remark_order").height() - 51 - 178 - 79,"width":570});
    		$t.$element.find(".modal-list").css({"height":"auto","width":570});
    		
//    		折扣查询
    		if(!$.isFunction($().discountSearch)){
                $.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/frontpos/posorderDiscountSearch.js");
	            $.ajaxSetup({ async: true });
			}
    		$("").discountSearch({
    			no:$opt.no?$opt.no:"",//no号
    			getDefaultBack:function(data){
//    				console.log(data);
    				$t.couponData(data);
    				$t.bindEvent();
    			}
    		});
    	},
    	
    	//优惠详情赋值
    	couponData:function(data){
    		var $t=this;
    		var $opt=$t.options;
    		if(data.ct_brzd.length > 0){
    			if(!!data.ct_brzd[0].distext){
        			$t.$element.find("#coupon_type input").val(data.ct_brzd[0].distext);
        		}
        		if(!!data.ct_brzd[0].disrate){
        			$t.$element.find("#coupon_rate input").val(data.ct_brzd[0].disrate);
        		}
    		}
//    		$t.$element.find("#coupon_remark textarea").val(data.ct_brzd[0].discode);
    		
			$t.$element.find("#coupon_type button").attr("data-json",JSON.stringify(data.ct_discode));
			$t.$element.find("#coupon_rate button").attr("data-json",JSON.stringify(data.discoderate));
			$t.$element.find("#coupon_remark button").attr("data-json",JSON.stringify(data.discodetype));
    	},
    	
    	//绑定事件
    	bindEvent:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		//输入
//    		el.find("#coupon_remark").on("focus","input",function(){
//    			window.location.href = "handwriter://"; 
//    		});
//    		el.find("#coupon_type").on("focus","input",function(){
//    			window.location.href = "handwriter://"; 
//    		});
    		
    		//输入折扣率
    		el.find("#coupon_rate input").bind("click",function(){
    			//计算器
				var status = false;
				if(!$.isFunction($().digitalCalculator)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/frontpos/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				$("#mykeyboard").digitalCalculator({
					number:el.find("#coupon_rate input").val()?el.find("#coupon_rate input").val():"",//折扣率
					status:status?status:false,
					title:$opt.title?$opt.title:"",
					type : "discount",
					//初始化数据返回回调
					dataBack:function(number){
 						el.find("#coupon_rate input").val(number);
					}
				});
    		});
    		
    		//选择
    		el.find(".modal-coupon").on("click","button",function(){
    			if(!$(this).hasClass("disabled")){
    				var data = JSON.parse($(this).attr("data-json"));
        			var id = $(this).parents(".input-group").attr("id");
        			$t.$element.find(".modal-list .orderTaste").empty();
        			if(data.length > 0){
        				if(Math.ceil(data.length / 4) > 4){
            				$t.$element.find(".modal-list .orderTaste").css({"width":504});
            			}else{
            				$t.$element.find(".modal-list .orderTaste").css({"width":578});
            			}
    					$(data).each(function(key,value){
        					var btn_class = "btn-light";
//            					if(item == value.mc){
//        							btn_class = "btn-primary";
//        						}
        					if(id == "coupon_type"){
            					var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'" data-fixed="'+ value.fixed +'" data-rate="" data-code="'+ value.discode +'">'+ value.text +'</button>');
            					if(!!value.rate){
            						btn_html.attr("data-rate",value.rate);
            					}
            					if(value.text.length > Math.floor((($t.$element.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",(($t.$element.find(".orderTaste").width() - 16) / 4) / value.text.length);
            					}
            					$t.$element.find(".modal-list .orderTaste").append(btn_html).attr("data-type","type");
        					}else if(id == "coupon_rate"){
        						var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'">'+ value.descript1 +'</button>');
        						if(value.descript1.length > Math.floor((($t.$element.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",(($t.$element.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
            					}
        						$t.$element.find(".modal-list .orderTaste").append(btn_html).attr("data-type","rate");
        					}else if(id == "coupon_remark"){
        						var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'">'+ value.descript1 +'</button>');
        						if(value.descript1.length > Math.floor((($t.$element.find(".orderTaste").width() - 16) /4) / 18)){
            						btn_html.css("font-size",(($t.$element.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
            					}
        						$t.$element.find(".modal-list .orderTaste").append(btn_html).attr("data-type","remark");
        					}
            			});
        				$t.$element.find(".modal-list button").css({"width":($t.$element.find(".orderTaste").width() - 16) / 4,"height":44.5});
        			}else{
        				if($(this).parents(".input-group").attr("id") == "coupon_type"){
        					jf_alert("无折扣类型可选~");
    					}else if($(this).parents(".input-group").attr("id") == "coupon_rate"){
    						jf_alert("无折扣率可选~");
    					}else if($(this).parents(".input-group").attr("id") == "coupon_remark"){
    						jf_alert("无折原因可选~");
    					}
        			}
    			}
    		});
    		
    		//选择赋值
    		el.find(".orderTaste").on("click","button",function(){
    			if($(this).parents(".orderTaste").attr("data-type") == "type"){
    				el.find("#coupon_type input").val($(this).text());
    				if($(this).attr("data-fixed") == "1"){
    					el.find("#coupon_rate button").removeClass("disabled");
    					if(!!$(this).attr("data-rate")){
    						el.find("#coupon_rate input").val($(this).attr("data-rate"));
    					}
    					el.find("#coupon_type input").attr("data-code",$(this).attr("data-code"));
    				}else{
    					el.find("#coupon_rate button").addClass("disabled");
    				}
    			}else if($(this).parents(".orderTaste").attr("data-type") == "rate"){
    				el.find("#coupon_rate input").val($(this).text());
    			}else if($(this).parents(".orderTaste").attr("data-type") == "remark"){
    				el.find("#coupon_remark textarea").val($(this).text());
    			}
    			el.find(".orderTaste").empty();
    		});
    		
    		//取消
    		el.find("#btnCancel").bind("click",function(){
    			$opt.returnBack();
    		});
    		
    		//确定
    		el.find("#btnSure").bind("click",function(){
    			var ct_brzd = {"no":$opt.no,"discode":el.find("#coupon_type input").attr("data-code"),"disrate":el.find("#coupon_rate input").val(),"str4":el.find("#coupon_remark textarea").val()};
    			$t.saveOper(ct_brzd);
    		});
    		
    		//取消全单
    		el.find("#modalBtnCancelDisrate").unbind("click").click(function(){
    			ct_brzd = {"no":$opt.no,
    						"discode":"*",
    						"disrate":"1.00",
    						"str4":""};
    			$t.saveOper(ct_brzd);
    		})
    	},
    	saveOper : function(ct_brzd){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		$t.wait();
    		var postData={};
			postData.ct_brzd = ct_brzd;
    		$.ajax({
    			url: $.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
    			type: "post",
    			dataType: "json",
    			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSCT_brzdEditSave',"{}"),
    			success : function(data){
    				$t.goon();
					if(data.RETURN != -1){
    					$opt.getDefaultBack();
    				}else{
    					jf_alert(data.MSG);
    				}
    			},
    			error : function(data){
    				$t.goon();
        			jf_alert(JSON.stringify(data));
    			},
    		})
    	},
		//返回筛选器对象,用于链式操作
    	hsOle: function() {
            return this.$element;      
        }, 
        goon : function(){
			var el = this.$element;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
		wait : function(){
			var el = this.$element;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		}

    }
})(jQuery);